package org.mclll.xwangplugin.town.config;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.mclll.xwangplugin.log.GlobalLog;
import org.mclll.xwangplugin.log.LogUtil;
import org.mclll.xwangplugin.log.LoggerFactory;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Set;

/**
 * @author: geduo
 * @date: 2023/9/23 11:59
 */
public class TownContext {
    public static LogUtil log;
    public static final String CONFIG_FILE_NAME = "townConfig.yml";
    public static String ONLY_PLAYER_COMMAND = "§c只有玩家才能执行该命令";
    public static String NOT_PERMISSION = "§c你没有权限执行该命令";
    public static String UNKNOWN_COMMAND = "§c未知的命令";
    public static String COMMAND_HELP =
            "§e【---- poitown使用帮助 ----】\n" +
            "§e【城镇列表\n" +
            "§2/poitown townList\n" +
            "§e【申请加入城镇\n" +
            "§2/poitown applyJoin <城镇名>\n" +
            "§e【镇长邀请玩家加入城镇\n" +
            "§2/poitown invite <玩家名称>"+
            "§e【镇长上交城镇税\n" +
            "§2/poitown submitTax <整数：心之石数量>";
    public static String OP_ONLY_COMMAND_HELP =
            "\n§e【派遣玩家加入城镇\n" +
            "§2/poitown deploy <玩家名> <城镇名> [confirm]\n" +
            "§e【创建新城镇\n" +
            "§2/createNewTown <城镇名> <城镇阵营> <领地列表> <镇长名称>\n" +
            "§e【摧毁城镇\n" +
            "§2/poitown destroy <城镇名> [confirm]\n" +
            "§e【踢出镇民\n" +
            "§2/poitown kick <玩家名> [confirm]\n" +
            "§e【城邦易主\n" +
            "§2/poitown owner <玩家名> <城镇名> [confirm]\n" +
            "§e【设置城镇没有镇长\n" +
            "§2/poitown noneLeader <城镇名> [confirm]\n" +
            "§e【打开城镇传送菜单(可指定玩家)\n" +
            "§2/poitown warp [玩家名]\n"+
            "§e【设置城镇传送位置，手持物品为图标，OP所在位置为传送点\n" +
            "§2/poitown setWarp <城镇名> <传送title>\n" +
            "§e【为城镇玩家签到\n" +
            "§2/poitown poi <玩家名>\n" +
            "§e【为玩家打开城镇签到\n" +
            "§2/poitown shop [玩家名]\n" +
            "§e【为城镇商店添加手上的商品\n" +
            "§2/poitown shopAdd <城镇名> <number:物品金币单价> <number:花费金币一次性最多购买物品数量> <number:一心之石购买数量>\n" +
            "§e【重载配置\n" +
            "§2/poitown reload";
    public static String TOWN_LIST_TITLE = "§e【---- 城镇列表 ----】";
    public static String TOWN_LIST_LINE = "§b -  %s     ";
    public static String TOWN_DETAIL_TITLE = "§e【---- %s详情 ----】";
    public static String TOWN_DETAIL_LINE_1 = "§b - 城镇人数: ";
    public static String TOWN_DETAIL_LINE_2 = "§b - 镇长: %s";
    public static String TOWN_DETAIL_LINE_3 = "§b - 城镇等级: %s";
    public static String TOWN_DETAIL_LINE_4 = "§b - 城镇阵营: %s";
    public static String TOWN_DETAIL_LINE_5 = "§b - 本月纳税: %d/%d个心之石";
    public static String TOWN_DETAIL_LINE_6 = "§b - 几计纳税: %d个心之石";
    public static String APPLY_LIST_TITLE = "§e【---- 申请列表 ----】";
    public static String DEPLOY_PLAYER_COMMAND_FORMAT = "§c格式错误，正确格式: poitown deploy <玩家名> <城镇名>";
    public static String KICK_PLAYER_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown kick <玩家名>";
    public static String CREATE_NEW_TOWN_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown createNewTown <城镇名> <城镇阵营> <领地列表> <镇长名称>";
    public static String INVITE_PLAYER_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown invite <玩家名>";
    public static String PLAYER_APPLY_JOIN_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown applyJoin <城镇名>";
    public static  String ACCEPT_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown accept <申请id>";
    public static  String REJECT_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown reject <申请id>";
    public static  String OWNER_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown owner <玩家名> <城镇名>";
    public static  String NONE_LEADER_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown noneLeader <城镇名>";
    public static  String DESTROY_TOWN_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown destroy <城镇名>";
    public static  String TOWN_TP_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown tp [玩家名]";
    public static  String TOWN_SET_TP_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown setTp <城镇名> <传送标题>";
    public static  String TOWN_SIGN_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown poi <城镇名> <玩家名>";
    public static  String TOWN_SHOP_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown shop <城镇名> [玩家名]";
    public static  String TOWN_SHOP_ADD_COMMAND_FORMAT = "§c格式错误，正确格式为：poitown shopAdd <城镇名> <数字：物品金币单价> <整数：花费金币一次性最多购买物品数量> <整数：一心之石购买数量>";
    public static  String TOWN_SUBMIT_TAX_FORMAT = "§c格式错误，正确格式为：poitown submitTax <整数：心之石数量>";
    public static  String TOWN_SET_DESC_FORMAT = "§c格式错误，正确格式为：poitown setDesc <城镇名> <文本>";
    public static String DEPLOY_PLAYER_CONFIRM = "§c确认将【%s】派到【%s】？";
    public static String KICK_PLAYER_CONFIRM = "§c确认将【%s】踢出城镇？";
    public static String OWNER_CONFIRM = "§c确认将【%s】提拔为【%s】的镇长？原来的镇长将贬为普通玩家！！";;
    public static String NONE_LEADER_CONFIRM = "§c确认将【%s】的镇长革职？";
    public static String DESTROY_TOWN_CONFIRM = "§c确认将【%s】摧毁？";
    public static String LEAVE_TOWN_CONFIRM = "§c确认离开城镇【%s】？";
    public static  String TOWN_SUBMIT_TAX_SUCCESS = "§a 成功缴纳%d个心之石";
    public static String DEPLOY_PLAYER_SUCCESS = "§a成功将【%s】派到【%s】";
    public static String KICK_PLAYER_SUCCESS = "§a【%s】已被踢出城镇";
    public static String CREATE_NEW_TOWN_SUCCESS = "§a城镇【%s】创建成功";
    public static String INVITE_PLAYER_SUCCESS = "§a已邀请【%s】加入城镇， 等待玩家同意";
    public static String PLAYER_JOIN_TOWN_SUCCESS = "§a玩家【%s】已加入城镇";
    public static String PLAYER_REJECT_APPLY_SUCCESS = "§a 已拒绝加入城镇【%s】";
    public static String LEADER_REJECT_APPLY_SUCCESS = "§a 已拒绝【%s】的申请";
    public static String PLAYER_APPLY_JOIN_SUCCESS = "§a 已申请【%s】，请等待镇长审批";
    public static String DESTROY_TOWN_SUCCESS = "§a 城镇【%s】已摧毁";
    public static String NONE_LEADER_SUCCESS = "§a 城镇【%s】已成为无主之地";
    public static String OWNER_SUCCESS = "§a 玩家【%s】已提拔为城镇【%s】的镇长";
    public static String TOWN_SET_TP_SUCCESS = "§a 城镇【%s】星门传送设置成功";
    public static String TOWN_SHOP_ADD_SUCCESS = "§a 成功为【%s】上架了一件商品";
    public static String TOWN_SET_DESC_SUCCESS = "§a 成功为【%s】设置了备注";
    public static String DEPLOY_PLAYER_SUCCESS_SEND_PLAYER = "§a您已被派到【%s】";
    public static String KICK_PLAYER_SUCCESS_SEND_PLAYER = "§a您已被踢出【%s】";
    public static String CREATE_NEW_TOWN_SUCCESS_SEND_LEADER = "§a您已成为【%s】的镇长";
    public static String INVITE_PLAYER_SUCCESS_SEND_PLAYER = "§a您被邀请加入【%s】    ";
    public static String PLAYER_JOIN_TOWN_SUCCESS_SEND_PLAYER = "§a您已加入【%s】";
    public static String LEADER_CANT_LEAVE = "§c镇长不能主动离开城镇";
    public static String PLAYER_IS_ALREADY_LEADER = "§c该玩家已经是镇长了";
    public static String TOWN_NAME_IS_ALREADY_EXIST = "§c已【%s】命名的城镇名已经存在";
    public static String PLAYER_ALREADY_HAVE_TOWN = "§c【%s】已经属于一个城镇了";
    public static String SIGN_ONLY_YOUR_TOWN = "§c 不属于该城镇，请在自己的所属城镇签到";
    public static String RES_IS_REGISTERED = "§c领地【%s】属于其他城镇";
    public static String PLAYER_ALREADY_APPLY_JOIN = "§c你加入【%s】的申请正在审批中";
    public static String PLAYER_NO_HAVE_TOWN = "§c【%s】不属于任何城镇";
    public static String NO_HAVE_TOWN = "§c你不属于任何城镇";
    public static String TOWN_NOT_FOUND = "§c未找到该城镇";
    public static String PLAYER_NOT_FOUND = "§c未匹配到任何玩家";
    public static String APPLY_ID_NOT_FOUND = "§c未找到该申请记录";
    public static String TOWN_PARTY_NOT_FOUND = "§c错误的阵营！破晓：DAWN、蓬莱：FAIRYLAND";
    public static String SIGN_SUCCESS = "§a签到成功";
    public static String DUPLICATE_SIGN = "§c你今天已经签到过了!";
    public static String BACKPACK_IS_FULL = "§c 背包已满，请留出足够的空间";
    public static String TOWN_SHOP_CALL_BACK = "§a 您获得一笔来自城镇商店的收益￥【%.2f】";
    public static String NUMBER_PARSE_ERROR = "§c 【%s】 不是一个数字，请重新输入";
    public static String MONEY_NOT_ENOUGH = "§c 金币不足，未完成购买";
    public static String NO_HAVE_HEART_STONE = "§c 心之石不足";
    public static String ITEM_NOT_FOUND_MAIN_HAND = "§c 请将要出售的物品拿在主手上";
    public static String TOWN_TAX_ALERT = "§c 月底收租！本月还有 %d 个心之石未缴纳";
    public static String NOW_TOWN_DUST = "§c 【%s】城镇已废墟化";
    public static String NOW_TOWN_LEVEL_DOWN = "§c 【%s】城镇已降级";
    public static String PLAYER_OFFLINE = "§c 玩家【%s】不在线";
    public static String SAVED_RECEIVE_SUCCESS = "§a 您已领取暂存的【%d】个心之石";
    public static String YOU_HAVE_BEEN_DISMISSED = "§a你已被革去镇长之职，恢复【旅行者】身份";
    public static String CONFIRM_BUTTON = "§a[确认]";
    public static String CANCEL_BUTTON = "§c[取消]";
    public static String DETAIL_BUTTON = "§n【详情】";
    public static String APPLY_BUTTON = "§n【申请】";

    public static void setLogger(){
        log = LoggerFactory.get(GlobalLog.class);
    }
    public static void reloadConfig(File configFolder) {
        YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(configFolder, CONFIG_FILE_NAME));
        ConfigurationSection context = config.getConfigurationSection("CONTEXT");
        Set<String> keys = context.getKeys(false);
        if (keys.isEmpty()) {
            log.info("未找到配置文件: {0}", CONFIG_FILE_NAME);
            return;
        }
        Class clazz = TownContext.class;
        for (String key : keys) {
            try {
                Field field = clazz.getDeclaredField(key);
                field.setAccessible(false);
                field.set(null, context.getString(key));
            } catch (NoSuchFieldException | IllegalAccessException e) {
                log.error("配置文件: {0} 中的字段: {1} 未找到", CONFIG_FILE_NAME, key);
                continue;
            }
        }
        context = config.getConfigurationSection("CONFIG");
        keys = context.getKeys(false);
        if (keys.isEmpty()) {
            log.info("未找到配置文件: {0}", CONFIG_FILE_NAME);
            return;
        }
        clazz = TownConfig.class;
        for (String key : keys) {
            try {
                Field field = clazz.getDeclaredField(key);
                field.setAccessible(false);
                Class<?> type = field.getType();
                if (type == Integer.class) {
                    field.set(null, context.getInt(key));
                } else if (type == Double.class) {
                    field.set(null, context.getDouble(key));
                }
            } catch (NoSuchFieldException | IllegalAccessException e) {
                log.error("配置文件: {0} 中的字段: {1} 未找到", CONFIG_FILE_NAME, key);
            }
        }
    }


}
